/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.yipinsoft.modules.wechat.core.api;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.thinkgem.jeesite.common.persistence.ResponseResult;
import com.thinkgem.jeesite.common.utils.CacheUtils;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.modules.test.entity.VehicleEHS3Result;
import com.thinkgem.jeesite.modules.test.service.JedisService;

import me.chanjar.weixin.mp.bean.result.WxMpUser;

/**
 * 登录Controller
 * 
 * @author ThinkGem
 * @version 2013-5-31
 */
@RestController
@RequestMapping(value = "${wechatPath}")
public class BusinessApiController extends BaseController {
	private static final String WECHAT_USER_CACHE = "wechatUserCache";
	
	@Autowired
	private JedisService jedisService;
	
	/**
	 * 微信认证后，api测试地址
	 * 
	 * @return
	 */
	@RequestMapping(value = "/getWechatToken", method = RequestMethod.GET)
	public WxMpUser getWechatToken(HttpServletRequest request, HttpServletResponse response) {

		String openId = request.getHeader("openid");

		if (StringUtils.isNotBlank(openId) && CacheUtils.get(WECHAT_USER_CACHE, openId) != null) {
			return (WxMpUser)CacheUtils.get(WECHAT_USER_CACHE, openId);
		}
		
		return null;
	}
	
	@RequestMapping(value = {"queryVehicleTrackInfoByTerminalCode", "" }, method = RequestMethod.POST)
	public ResponseResult queryVehicleTrackInfoByTerminalCode(@RequestBody VehicleEHS3Result vehicle) throws Exception {
		try{
			List<VehicleEHS3Result> list = jedisService
					.queryEHS3VehicleTrackInfoByTerminalCode(vehicle.getTerminalCode(), vehicle.getStartTime(), vehicle.getEndTime());
			return ResponseResult.success(list);
		}catch(Exception ex){
			return ResponseResult.failure(ex.getMessage());
		}
	}
}
